package com.meme.editor.level;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ListIterator;

import com.meme.editor.Box2d;
import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;
import com.meme.editor.level.define.DefineFile;
import com.meme.editor.model.sprite2d.AnimatedModule;
import com.meme.editor.model.sprite2d.AnimatedModuleGroup;
import com.meme.editor.model.sprite2d.Sprite2dFile;
import com.meme.editor.model.sprite2d.Tiled2dBackground;
import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.value.EntityLinkPropertyValue;
import com.meme.editor.renderer.Tiled2dBackgroundRenderer;
import com.meme.io.editor.FileCache;
import com.meme.util.FileUtil;

public class Background2dAppearance extends Appearance
{

	public static final String ParamName_Sprite2dFile = "sprite2d_file";
	public static final String ParamName_BackgroundName = "background_name";
	
	final static String GroupName_SpriteGroup = "Sprite Properties";
	final static String PropertyName_Background        = "Background";

	
	
	Sprite2dFile mSprite2dFile;
	Sprite2dFile.TiledBackgroundSet mTiledBackgroundSet;
	Tiled2dBackground mTiled2dBackground;

	public Background2dAppearance()
	{
		super ();
		createPublicProperty (GroupName_SpriteGroup, PropertyName_Background, PropertyFactory.ValueType_EntityRef );
	}
	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{			
		super._handleModifyingPropertyValues (modications);
		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if ( mod.mGroupName.equals (GroupName_SpriteGroup) )
			{
				boolean handled = true;
				if ( mod.mPropertyName.equals (PropertyName_Background) )
				{
					setBackground ( (Tiled2dBackground) (mod.mNewValue) );
					iter.remove();
				}
					
			}
		}
	}
	
	public void setBackground (Tiled2dBackground background)
	{
		mTiled2dBackground = background;
		changePublicPropertyValue (GroupName_SpriteGroup, PropertyName_Background, mTiled2dBackground);
	}
	
	public Tiled2dBackground getBackground ()
	{
		return mTiled2dBackground;
	}
	
	
	public Tiled2dBackground getTiled2dBackground ()
	{
		return mTiled2dBackground;
	}
	
	@Override
	public void setParameter (String paramName, String paramValue)
	{
		if (paramName.equals(ParamName_Sprite2dFile))
		{
			DefineFile defineFile = ( (LevelFile) getHost () ).getDefineFile();
			
			String filepath = FileUtil.getFullPathFilename(paramValue, defineFile.getHostFilename());
			
			mSprite2dFile = FileCache.getSprite2dFile (filepath);
			mTiledBackgroundSet = mSprite2dFile.getTiledBackgroundSet();
			
			getPublicProperty(GroupName_SpriteGroup, PropertyName_Background)
			        .setParameter (EntityLinkPropertyValue.Param_NodePath, "$Sprite2dFile/" + mTiledBackgroundSet.getName() + "/$_children");

		}
		else if (paramName.equals(ParamName_BackgroundName))
		{
			if (mTiledBackgroundSet != null)
			{
				setBackground ( (Tiled2dBackground) (mTiledBackgroundSet.getChild (paramValue)) );
			}
		}
	}
	
	@Override
	public Object getProperty (String propertyName)
	{
		if (propertyName.equals("Sprite2dFile"))
		{
			return mSprite2dFile;
		}
		
		return super.getProperty (propertyName);
	}
	
//	@Override
//	public void show (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flag)
//	{
//		if (mTiled2dBackground != null)
//			Tiled2dBackgroundRenderer.renderTiled2dBackground(g, mTiled2dBackground, posX, posY, scale);
//	}
	
	@Override
	public void show (Graphics2D g, int flag)
	{
		if (mTiled2dBackground != null)
			Tiled2dBackgroundRenderer.renderTiled2dBackground(g, mTiled2dBackground, 0, 0, 1);
	}
	
	@Override
	public void getBoundingBox (Box2d box2d)
	{
		if (mTiled2dBackground != null)
		{
			int cell_rows = mTiled2dBackground.getCellRows();
			int cell_cols = mTiled2dBackground.getCellCols();
			int cell_width = mTiled2dBackground.getCellWidth();
			int cell_height = mTiled2dBackground.getCellHeight();
			
			box2d.set(0, 0, cell_cols * cell_width, cell_rows * cell_height);
		}
		else
			box2d.reset();
		
//		Actor actor = (Actor) getParent ();
//		box2d.adjust (actor.isFlipX (), actor.isFlipY ());
//		box2d.shift(actor.getPosX(), actor.getPosY());
	}
}
